package com.lz.app.demo.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSONObject;
import com.lz.app.demo.annotations.TokenRequired;
import com.lz.app.demo.dao.TblLanzuoSignDao;
import com.lz.app.demo.domain.TblLanzuoSign;
import com.lz.app.demo.dto.TblLanzuoSignResponseDto;
import com.lz.app.demo.dto.request.SignRequestDto;
import com.lz.app.demo.dto.response.SignRangeResponseDto;
import com.lz.app.demo.dto.response.SignStatusResponseDto;
import com.lz.app.demo.util.LocationUtil;
import com.lz.app.demo.util.mapper.BeanMapperTemplate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Title: QdController
 * @ProjectName app-service
 * @Author huchao
 * @Date 2018/9/4上午10:53
 */
@RestController
@RequestMapping(value = "/sign")
@Slf4j
@TokenRequired
@Api(description = "签到")
public class SignController {

	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");


	@Autowired
	private TblLanzuoSignDao tblLanzuoSignDao;

	@Autowired
	private BeanMapperTemplate beanMapperTemplate;


	/**
	 * 获取签到基础数据
	 *
	 * @return
	 */
	@RequestMapping(value = "range", method = RequestMethod.GET)
	@ApiOperation(value = "获取签到基础数据")
	public SignRangeResponseDto range() {
		return new SignRangeResponseDto();
	}

	/**
	 * 按日获取签到流水
	 *
	 * @param httpServletRequest
	 * @param day
	 * @return
	 */
	@RequestMapping(value = "/{day}/day", method = RequestMethod.GET)
	@ApiOperation(value = "按日获取签到流水")
	public SignStatusResponseDto status(HttpServletRequest httpServletRequest, @PathVariable(value = "day") int day) {
		SignStatusResponseDto qdStatusResponse = new SignStatusResponseDto();
		Integer userId = (Integer) httpServletRequest.getAttribute("userId");
		List<TblLanzuoSign> list = tblLanzuoSignDao.findByUserIdAndCreateDayOrderByCreateTsDesc(userId, day);
		if (CollectionUtils.isEmpty(list)) {
			qdStatusResponse.setStatus(0);
		} else {
			long count = list.stream().filter(row -> row.getType() == 2).count();
			if (count > 0) {
				qdStatusResponse.setStatus(2);
			} else {
				qdStatusResponse.setStatus(1);
			}
		}
		qdStatusResponse.setRecords(beanMapperTemplate.mapToList(list, TblLanzuoSignResponseDto.class));
		return qdStatusResponse;
	}

	/**
	 * 签入/签出
	 *
	 * @param httpServletRequest
	 * @param signRequestDto
	 * @return
	 */
	@RequestMapping(value = "", method = RequestMethod.POST)
	@ApiOperation(value = "签入/签出")
	public TblLanzuoSignResponseDto qdIn(HttpServletRequest httpServletRequest, @RequestBody SignRequestDto signRequestDto) {
		log.info("qdIn requestDto:" + JSONObject.toJSONString(signRequestDto));
		SignRangeResponseDto signRangeResponseDto = new SignRangeResponseDto();


		Integer userId = (Integer) httpServletRequest.getAttribute("userId");
		Calendar calendar = Calendar.getInstance();
		Integer day = Integer.valueOf(sdf.format(calendar.getTime()));
		List<TblLanzuoSign> list = tblLanzuoSignDao.findByUserIdAndCreateDayOrderByCreateTsDesc(userId, day);

		TblLanzuoSign tblLanzuoSign = new TblLanzuoSign();
		tblLanzuoSign.setUserId(userId);
		tblLanzuoSign.setCreateTs(calendar.getTime());
		tblLanzuoSign.setLongitude(signRequestDto.getLongitude());
		tblLanzuoSign.setLatitude(signRequestDto.getLatitude());
		tblLanzuoSign.setDesc(signRequestDto.getDesc());
		tblLanzuoSign.setCreateDay(day);


		//经纬度范围判断
		double meter = LocationUtil.distanceByLongNLat(signRangeResponseDto.getLongitude(), signRangeResponseDto.getLatitude(),
				signRequestDto.getLongitude(), signRequestDto.getLatitude());
		if (meter > signRangeResponseDto.getRange()) {
			tblLanzuoSign.setRangeNormal(0);
		} else {
			tblLanzuoSign.setRangeNormal(1);
		}

		//签到时间判断
		if (CollectionUtils.isEmpty(list)) {
			tblLanzuoSign.setType(1);

			int signInHour = Integer.valueOf(signRangeResponseDto.getInTime().substring(0, 2));
			int signInMin = Integer.valueOf(signRangeResponseDto.getInTime().substring(2, 4));
			Calendar normalCal = Calendar.getInstance();
			normalCal.set(Calendar.HOUR_OF_DAY, signInHour);
			normalCal.set(Calendar.MINUTE, signInMin);
			if (calendar.compareTo(normalCal) > 0) {
				tblLanzuoSign.setTimeNormal(0);
			} else {
				tblLanzuoSign.setTimeNormal(1);
			}
		} else {
			tblLanzuoSign.setType(2);

			int signOutHour = Integer.valueOf(signRangeResponseDto.getOutTime().substring(0, 2));
			int signOutMin = Integer.valueOf(signRangeResponseDto.getOutTime().substring(2, 4));

			Calendar normalCal = Calendar.getInstance();
			normalCal.set(Calendar.HOUR_OF_DAY, signOutHour);
			normalCal.set(Calendar.MINUTE, signOutMin);

			if (calendar.compareTo(normalCal) < 0) {
				tblLanzuoSign.setTimeNormal(0);
			} else {
				tblLanzuoSign.setTimeNormal(1);
			}
		}
		tblLanzuoSign = tblLanzuoSignDao.save(tblLanzuoSign);
		return beanMapperTemplate.map(tblLanzuoSign, TblLanzuoSignResponseDto.class);
	}

}
